﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace UFrame
{
    public class SysMsgReciever : MonoBehaviour
    {
        public Image mRedImg;

        [SerializeField]
        private List<ESysRedType> mListType = new List<ESysRedType>();

        [SerializeField]
        private string mGuid = string.Empty;

        public List<ESysRedType> ListType
        {
            get
            {
                return mListType;
            }
        }

        private bool mInit = false;

        public void Awake()
        {
            if (null == mRedImg)
            {
                mRedImg = GetComponent<Image>();
            }

            mRedImg.gameObject.SetActive(true);
            SysMsgMgr.I.AddReciever(this);
            mInit = false;
        }

        public void OnEnable()
        {
            if (mInit)
            {
                return;
            }

            Refresh();
            mInit = true;
        }

        public void OnDestroy()
        {
            SysMsgMgr.I.RemoveReceiver(this);
            mInit = false;
        }

        public void Refresh()
        {
            bool bShow = false;
            List<ESysRedType>.Enumerator ie = mListType.GetEnumerator();

            while (ie.MoveNext())
            {
                if (SysMsgMgr.I.HasNew(ie.Current, mGuid))
                {
                    bShow = true;
                    break;
                }
            }

            Notify(bShow);
        }

        public void Refresh(ESysRedType type)
        {
            bool bShow = SysMsgMgr.I.HasNew(type, mGuid);
            Notify(bShow);
        }



        public bool IsContain(ESysRedType type)
        {
            return mListType.Contains(type);
        }

        public void Notify(ESysRedType leaf, bool isMsg)
        {
            bool b = false;
            for (int i = 0; i < mListType.Count; i++)
            {
                if (mListType[i] == leaf || SysMsgMgr.I.IsParent(mListType[i], leaf))
                {
                    b = true;
                    break;
                }
            }

            if (b == false)
            {
                return;
            }

            Notify(isMsg);
        }

        public void Notify(bool isMsg)
        {
            if (mRedImg.gameObject.activeInHierarchy == isMsg && mRedImg.enabled == isMsg)
            {
                return;
            }

            //mRedImg.gameObject.SetActive(b);
            mRedImg.enabled = isMsg;
        }

    }
}